Declare Function nlWinSetTabStops Lib "User" Alias "SendMessage" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, lParam As Any) As Long
Declare Function nlWinAPI_GetTextExtent Lib "GDI" Alias "GetTextExtent" (ByVal hDC As Integer, ByVal lpString As String, ByVal nCount As Integer) As Long
Declare Function nlWinAPI_GetDialogBaseUnits Lib "User" Alias "GetDialogBaseUnits" () As Long
Global Const nWM_USER = 1024
Global Const nLB_SETTABSTOPS = nWM_USER + 19
Global Const nEM_SETTABSTOPS = nWM_USER + 27
Global Const nCB_SELECTSTRING = nWM_USER + 13
Global Const nLB_SELECTSTRING = nWM_USER + 13
Global Const nSEARCH_FROM_TOP = -1
Function AutoSetTabStopsCheck (ListCtrl As Control, TextCtrl As Control, tfUseHeadingWidthsOnly As Integer, SetDefaultTabs As Integer) As Integer
'This function automatically calculates and sets appropriate
'tabstops for a multi-column listbox, based on the actual data
'in the listbox. You do not have to tell the function how many
'columns you want, nor figure out how wide each column should be;
'the actual data placed into the listbox determines that.
'In addition to the listbox, the function also sets identical
'tabstops in an accompanying, multi-line textbox. This textbox
'provides the data for the column headings.
'tfUseHeadingWidthsOnly:
' True - Tabstops are calculated based only on the
' widths of the column headings. This option
' is must faster, but you're gambling that the
' actual data will always be narrower than the
' headings.
'
' False - Tabstops are calculated based on the widest
' entry in each column; both the headings and
' the data are examined. This option is slower
' because each entry in the listbox must be
' parsed, but it eliminates the guesswork.
'SetDefaultTabs:
' True - Tabstops are reset to Windows' default intervals
' of 8 dialog units.
'
' False - Tabstops are calculated based on the actual
' data in the listbox/textbox.
'
'
'The function itself returns FALSE if any of the control
'verification tests fail; otherwise it returns TRUE.
Dim sColHeadings As String, sColData As String, sColString As String
Dim sParentFontName As String, fParentFontSize As Single
Dim tfParentFontBold As Integer, tfParentFontItalic As Integer
Dim nColCount As Integer, nDataWidth As Integer, nSpaceBetweenCols As Integer
Dim nMaxListboxCols As Integer, nNbrListboxCols As Integer, nNbrTabstops As Integer
Dim nInStart As Integer, nTabPos As Integer
Dim nlistsub As Integer, nTabSub As Integer
Dim nlRC As Long
Dim nListFontAvgWidth As Integer, nSystemFontAvgWidth As Integer
Dim fListFontPixelsPerDlgUnit As Single, fFontRatio As Single
Dim i As Integer
Dim nColWidth() As Integer 'measured column widths
Dim nTabstop() As Integer 'calculated WinAPI tabstops
Dim Msg As String
'================
Main:
'================
GoSub VerifyControls
GoSub Initialize
If SetDefaultTabs Then
nNbrTabstops = 0
GoSub UpdateCtrls
Else
'Since VB provides an hDC property for forms, but
'not for controls, we must temporarily set the parent
'form's font characteristics equal to the listbox's
'font characteristics. Doing this ensures that all
'text measurements made using the form's DC will be
'accurate for the listbox.
sParentFontName = ListCtrl.Parent.FontName
fParentFontSize = ListCtrl.Parent.FontSize
tfParentFontBold = ListCtrl.Parent.FontBold
tfParentFontItalic = ListCtrl.Parent.FontItalic
ListCtrl.Parent.FontName = ListCtrl.FontName
ListCtrl.Parent.FontSize = ListCtrl.FontSize
ListCtrl.Parent.FontBold = ListCtrl.FontBold
ListCtrl.Parent.FontItalic = ListCtrl.FontItalic
'Identify and measure the width of the column headings
'present in the textbox.
GoSub MeasureColHeadingWidths
'Measure the width of the column data values present
'in the listbox.
If Not tfUseHeadingWidthsOnly Then
GoSub MeasureColDataWidths
End If
'Calculate and set the necessary tabstop values, based
'on the maximum width of each column.
GoSub UpdateCtrls
'Reset the parent form's font characteristics to their